package com.module.circle.ui.myMedal

import android.graphics.Typeface
import android.os.Bundle
import android.view.View
import androidx.core.content.ContextCompat
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.common.app.base.AppActivity
import com.common.app.data.bean.KeyBundle
import com.common.app.data.bean.KeyEvents
import com.common.app.data.bean.circle.CirclesMedalBadgesBean
import com.common.app.data.bean.circle.CirclesMedalModel
import com.common.app.helper.UserHelper
import com.common.app.utls.HeadTitleUtils
import com.common.base.app.extras.getShowMsg
import com.common.base.app.extras.load
import com.common.base.app.extras.no
import com.common.base.app.extras.otherwise
import com.common.base.app.extras.setOnClickListener
import com.common.base.app.extras.setText
import com.common.base.app.extras.setVisibility
import com.common.base.app.extras.showToast
import com.common.base.app.extras.startActivity
import com.common.base.app.extras.yes
import com.common.base.listener.AlertDialogConfirmBtnListener
import com.common.base.utils.JsonUtils
import com.common.base.utils.SpanUtils
import com.common.base.utils.StatusBarUtils
import com.common.base.utils.ViewUtils
import com.common.base.widget.animator.CustomAnimation1
import com.jeremyliao.liveeventbus.LiveEventBus
import com.module.circle.R
import com.module.circle.databinding.CircleActivityMembersListBinding
import com.module.circle.databinding.CircleActivityMyMedalBinding
import com.module.circle.ui.dialog.MedalInfoListAdapter
import com.module.circle.ui.medalDetails.MedalDetailsActivity
import com.module.circle.ui.selectDisplayMedal.SelectDisplayMedalActivity
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2025/08/13
 * <p>
 * 描述：我的勋章页面
 * <p>
 * 修订历史：
 */
class MyMedalActivity : AppActivity<CircleActivityMyMedalBinding, MyMedalViewModel>(),
    View.OnClickListener {

    @Autowired(name = KeyBundle.BUNDLE_KEY)
    @JvmField
    var mId: Int = 0

    override fun getViewModel() = MyMedalViewModel::class.java

    override fun getViewContentBinding() = CircleActivityMyMedalBinding.inflate(layoutInflater)

    override fun getPageTitle(): String = ""
    override fun isShowToolBar(): Boolean = false
    override fun isStatusBarTranslate(): Boolean = true


    private val classicsFooter by lazy { ClassicsFooter(this) }

    private val mAdapter by lazy { MedalInfoListAdapter(true) }

    private val headTitleUtils by lazy { HeadTitleUtils(this) }

    override fun initViews() {
        super.initViews()

        StatusBarUtils.setStatusBarHeight(mViewContentBinding.statusView)
        StatusBarUtils.setStatusBarForegroundColor(this, true)

        headTitleUtils.setTitle(getString(R.string.circle_my_medal_hint))
        headTitleUtils.setLineStyle(10)


        //上下拉刷新
        getRefreshFooter()?.let { mViewContentBinding.pdView.setRefreshFooter(it) }
        mViewContentBinding.pdView.setRefreshHeader(
            ClassicsHeader(this).setAccentColor(
                ContextCompat.getColor(this, R.color.text_content_week)
            )
        )
        classicsFooter.setAccentColor(
            ContextCompat.getColor(
                this,
                R.color.color_999999
            )
        )
        mViewContentBinding.pdView.setRefreshFooter(classicsFooter)

        mAdapter.apply {
            mViewContentBinding.hrv.adapter = this
        }
    }

    override fun initEvents() {
        super.initEvents()

        mViewContentBinding.pdView.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                lazyInitData()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                mViewContentBinding.pdView.setEnableLoadMoreWhenContentNotFull(false)
                mViewContentBinding.pdView.finishLoadMoreWithNoMoreData()
            }
        })
        mAdapter.setOnItemClickListener { adapter, view, position ->
            if (ViewUtils.isFastClick()) return@setOnItemClickListener //防止多次点击
            val bundle = Bundle()
            bundle.putParcelable(
                KeyBundle.BUNDLE_KEY,
                mAdapter.data[position]
            )
            startActivity<MedalDetailsActivity>(bundle, false)
        }

        mViewContentBinding.imgMedal1.setOnClickListener(onClick = this)
        mViewContentBinding.imgMedal2.setOnClickListener(onClick = this)
        mViewContentBinding.imgMedal3.setOnClickListener(onClick = this)
        mViewContentBinding.imgMedal4.setOnClickListener(onClick = this)

        LiveEventBus.get(KeyEvents.KEY_REFRESH_OUT_MEDAL_LIST).observe(this){
            getOutMedalList()
        }
    }

    override fun lazyInitData() {
        super.lazyInitData()
        getOutMedalList()
    }

    override fun onClick(v: View?) {
        when (v) {
            mViewContentBinding.imgMedal1 -> {
                val bundle = Bundle()
                bundle.putInt(KeyBundle.BUNDLE_KEY, UserHelper.getUser()?.user_id ?: 0)
                (mViewContentBinding.tvName1.tag !=null).yes {
                    bundle.putInt(KeyBundle.EXPAND_DATA, (mViewContentBinding.tvName1.tag as CirclesMedalBadgesBean).id?:0)
                }
                startActivity<SelectDisplayMedalActivity>(bundle, false)
            }
             mViewContentBinding.imgMedal2 -> {
                val bundle = Bundle()
                bundle.putInt(KeyBundle.BUNDLE_KEY, UserHelper.getUser()?.user_id ?: 0)
                 (mViewContentBinding.tvName2.tag !=null).yes {
                     bundle.putInt(KeyBundle.EXPAND_DATA, (mViewContentBinding.tvName2.tag as CirclesMedalBadgesBean).id?:0)
                 }
                startActivity<SelectDisplayMedalActivity>(bundle, false)
            }
            mViewContentBinding.imgMedal3 -> {
                val bundle = Bundle()
                bundle.putInt(KeyBundle.BUNDLE_KEY, UserHelper.getUser()?.user_id ?: 0)
                (mViewContentBinding.tvName3.tag !=null).yes {
                    bundle.putInt(KeyBundle.EXPAND_DATA, (mViewContentBinding.tvName3.tag as CirclesMedalBadgesBean).id?:0)
                }
                startActivity<SelectDisplayMedalActivity>(bundle, false)
            }
            mViewContentBinding.imgMedal4 -> {
                val bundle = Bundle()
                bundle.putInt(KeyBundle.BUNDLE_KEY, UserHelper.getUser()?.user_id ?: 0)
                (mViewContentBinding.tvName4.tag !=null).yes {
                    bundle.putInt(KeyBundle.EXPAND_DATA, (mViewContentBinding.tvName4.tag as CirclesMedalBadgesBean).id?:0)
                }
                startActivity<SelectDisplayMedalActivity>(bundle, false)
            }
        }
    }

    private fun finishPdView() {
        mViewContentBinding.pdView.finishRefresh()
        mViewContentBinding.pdView.finishLoadMore()
    }

    private fun setOutsideMedal(showList: List<CirclesMedalBadgesBean>) {

        initOutMedalList()

        showList.forEachIndexed { index, item ->
            when (index) {
                1 -> {
                    mViewContentBinding.imgMedal2.load(item.active_url, 0f)
                    mViewContentBinding.tvName2.text = item.name
                    mViewContentBinding.tvName2.tag = item
                    setVisibility(mViewContentBinding.imgMedalRepl2, true)
                }

                2 -> {
                    mViewContentBinding.imgMedal3.load(item.active_url, 0f)
                    mViewContentBinding.tvName3.text = item.name
                    mViewContentBinding.tvName3.tag = item
                    setVisibility(mViewContentBinding.imgMedalRepl3, true)
                }

                3 -> {
                    mViewContentBinding.imgMedal4.load(item.active_url, 0f)
                    mViewContentBinding.tvName4.text = item.name
                    mViewContentBinding.tvName4.tag = item
                    setVisibility(mViewContentBinding.imgMedalRepl4, true)
                }

                else -> {
                    mViewContentBinding.imgMedal1.load(item.active_url, 0f)
                    mViewContentBinding.tvName1.text = item.name
                    mViewContentBinding.tvName1.tag = item
                    setVisibility(mViewContentBinding.imgMedalRepl1, true)
                }
            }
        }
    }

    private fun initOutMedalList() {
        mViewContentBinding.imgMedal1.setImageResource(R.drawable.circle_medal_add_icon)
        setText(mViewContentBinding.tvName1, R.string.common_click_add_hint)
        setVisibility(mViewContentBinding.imgMedalRepl1, false)
        mViewContentBinding.imgMedal2.setImageResource(R.drawable.circle_medal_add_icon)
        setText(mViewContentBinding.tvName2, R.string.common_click_add_hint)
        setVisibility(mViewContentBinding.imgMedalRepl2, false)
        mViewContentBinding.imgMedal3.setImageResource(R.drawable.circle_medal_add_icon)
        setText(mViewContentBinding.tvName3, R.string.common_click_add_hint)
        setVisibility(mViewContentBinding.imgMedalRepl3, false)
        mViewContentBinding.imgMedal4.setImageResource(R.drawable.circle_medal_add_icon)
        setText(mViewContentBinding.tvName4, R.string.common_click_add_hint)
        setVisibility(mViewContentBinding.imgMedalRepl4, false)

        mViewContentBinding.tvName1.tag = null
        mViewContentBinding.tvName2.tag = null
        mViewContentBinding.tvName3.tag = null
        mViewContentBinding.tvName4.tag = null

    }

    private fun getMedalList() {
        if (mId <= 0) {
            showErrorMsg()
            return
        }
        showDialogLoading()
        mViewModel.getMedalList(mId) {
            showToast(it.getShowMsg())
            finishPdView()
        }.observe(this) { rsoIt ->
            finishPdView()
            dismissLoadingDialog()
            rsoIt.isSuccess().yes {
                rsoIt.Data?.let { dataIt ->
//                    JsonUtils.toJson(dataIt)
//                    val listTemp = dataIt.filter { it.current_level >= 1 }
                    mAdapter.setList(dataIt)
                }
            }.otherwise {
                showToast(rsoIt.Message)
            }
        }
    }

    private fun getOutMedalList() {
        if (mId <= 0) {
            showErrorMsg()
            return
        }
        showDialogLoading()
        mViewModel.getOutMedalList(mId) {
            showToast(it.getShowMsg())
            getMedalList()
            finishPdView()
        }.observe(this) { rsoIt ->
            getMedalList()
            finishPdView()
            dismissLoadingDialog()
            rsoIt.isSuccess().yes {
                rsoIt.Data?.let { dataIt ->
                    setOutsideMedal(dataIt)
                }
            }.otherwise {
                showToast(rsoIt.Message)
            }
        }
    }
}